package com.my.util;

import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

public class HttpUtil {

	protected final static Logger logger = LoggerFactory.getLogger(HttpUtil.class);

	// 创建默认的httpClient实例.
	protected final static CloseableHttpClient HTTP_CLIENT = HttpClients.createDefault();

	public static String httpPost(String url, String params) throws Exception {

		// 创建httppost
		HttpPost httppost = new HttpPost(url);
		logger.info("发送POST请求：url={},params={}", url, params);
		StringEntity s = new StringEntity(params,"UTF-8");
		s.setContentEncoding("UTF-8");    
		s.setContentType("application/json");
		httppost.setEntity(s);
		CloseableHttpResponse response = HTTP_CLIENT.execute(httppost);
		try {
			HttpEntity entity = response.getEntity();
			InputStream in = entity.getContent();
			String result = IOUtils.toString(entity.getContent(), "UTF-8");
			IOUtils.closeQuietly(in);
			return result;
		} finally {
			response.close();
		}

	}

	public static <T> T httpPost(String url, String params, Class<T> clazz) throws Exception {
		// 创建httppost
		HttpPost httppost = new HttpPost(url);
		logger.info("发送POST请求：url={},params={}", url, params);
		StringEntity s = new StringEntity(params,"UTF-8");
		s.setContentEncoding("UTF-8");    
		s.setContentType("application/json"); 
		httppost.setEntity(s);
		CloseableHttpResponse response = HTTP_CLIENT.execute(httppost);
		try {
			
				HttpEntity entity = response.getEntity();
				InputStream in = entity.getContent();
				String result = IOUtils.toString(entity.getContent(), "UTF-8");
				IOUtils.closeQuietly(in);
				return JSON.parseObject(result, clazz);
				
		} finally {
			response.close();
		}

	}

	public static String httpGet(String url) throws Exception {
		// 创建默认的httpClient实例.
		HttpGet httpGet = new HttpGet(url);
		logger.info("发送GET请求：url={}", url);
		CloseableHttpResponse response = HTTP_CLIENT.execute(httpGet);
		try {
			HttpEntity entity = response.getEntity();
			InputStream in = entity.getContent();
			String result = IOUtils.toString(entity.getContent(), "UTF-8");
			IOUtils.closeQuietly(in);
			return result;
		} finally {
			response.close();
		}
	}

	public static <T> T httpGet(String url, Class<T> clazz) throws Exception {
		// 创建默认的httpClient实例.
		HttpGet httpGet = new HttpGet(url);
		logger.info("发送GET请求：url={}", url);
		CloseableHttpResponse response = HTTP_CLIENT.execute(httpGet);
		try {
				HttpEntity entity = response.getEntity();
				InputStream in = entity.getContent();
				String result = IOUtils.toString(entity.getContent(), "UTF-8");
				IOUtils.closeQuietly(in);
				return JSON.parseObject(result, clazz);
		} finally {
			response.close();
		}
	}

}
